/*
 * Copyright (C) 2022 Unisoc Communications Inc All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef C2_UNISOC_DMA_BUFFER_H
#define C2_UNISOC_DMA_BUFFER_H

#include <utils/RefBase.h>
#include <BufferAllocator/BufferAllocatorWrapper.h>

namespace OHOS{
namespace android {

class C2UnisocDmaBuffer : public RefBase {
public:
    C2UnisocDmaBuffer(const char *heapName, size_t bufferLen);
    ~C2UnisocDmaBuffer();

    bool init();
    void allocateBuffer(const char *heapName, size_t bufferLen);
    int getDmaHeapID();
    void *getBase();

    static int GetPhyAddrFromDmaHeap(int bufferFd, unsigned long *phyAddr, size_t *size);
    int getPhyAddrFromDmaHeap(unsigned long *phyAddr, size_t *size);
    static void InvalidateDmaBuffer(int bufferFd);  // invalidate cache, call this function before cpu access
    int invalidateDmaBuffer(); // invalidate cache, call this function before cpu access
    static void FlushDmaBuffer(int bufferFd); // flush cache, call this fuction after cpu access
    void flushDmaBuffer(); // flush cache, call this fuction after cpu access
private:
    BufferAllocator *mBufferAllocator;
    int mFd;
    void *mBase;
    size_t mSize;
};

} // namespace android
} // namespace OHOS 

#endif // C2_UNISOC_DMA_BUFFER_H